package com.fastCms.business.bim.manage.impl;

import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.frame.constant.ResultCodeConst;
import com.fastCms.business.bim.daos.DeviceGroupMapper;
import com.fastCms.business.bim.manage.DeviceGroupManageService;
import com.fastCms.business.bim.pojo.entity.DeviceGroup;
import com.fastCms.business.bim.pojo.entity.DeviceGroupRel;
import com.fastCms.business.bim.pojo.po.DeviceGroupAddPO;
import com.fastCms.business.bim.pojo.po.DeviceGroupPO;
import com.fastCms.business.bim.pojo.po.DeviceGroupRelPO;
import com.fastCms.business.bim.pojo.vo.DeviceGroupVO;
import com.fastCms.business.bim.pojo.vo.DeviceInfoVO;
import com.fastCms.business.bim.service.DeviceGroupRelService;
import com.fastCms.business.bim.service.DeviceGroupService;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import com.fastCms.business.frame.utils.CommonsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional(rollbackFor = Exception.class)
@Component
@Slf4j
public class DeviceGroupManageServiceImpl implements DeviceGroupManageService {

    @Autowired
    private DeviceGroupMapper deviceGroupMapper;

    @Autowired
    private DeviceGroupService deviceGroupServiceImpl;

    @Autowired
    private DeviceGroupRelService deviceGroupRelServiceImpl;


    @Override
    public PageInfo<DeviceGroupVO> list(PageParam<DeviceGroupPO> pageParam) {
        DeviceGroupPO queryFields =
            pageParam.getQueryFields();
        PageInfo<DeviceGroupVO> result =
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize())
                .doSelectPageInfo(() ->
                    deviceGroupMapper.showList(queryFields)
                );
        return result;
    }

    @Override
    public ApiResponse<Boolean> saveOrUpdate(DeviceGroupAddPO deviceGroupAddPO) {
        if (null == deviceGroupAddPO.getId()) {
            DeviceGroup deviceGroup = new DeviceGroup();
            CommonsUtil.initEntity(deviceGroup);
            BeanUtils.copyProperties(deviceGroupAddPO, deviceGroup);
            boolean a = deviceGroupServiceImpl.save(deviceGroup);
            return ResponseUtil.success(a);
        }
        DeviceGroup deviceGroup = deviceGroupServiceImpl.findById(deviceGroupAddPO.getId());
        BeanUtils.copyProperties(deviceGroupAddPO, deviceGroup);
        deviceGroupServiceImpl.updateById(deviceGroup);
        return ResponseUtil.success(true);
    }

    @Override
    public ApiResponse<Boolean> delete(Integer id) {
        boolean b = deviceGroupServiceImpl.removeById(id);
        return ResponseUtil.success(b);
    }

    @Override
    public ApiResponse<Boolean> relDevice(DeviceGroupRelPO deviceGroupRelPO) {
        Integer groupId = deviceGroupRelPO.getGroupId();
        List<Integer> deviceIds = deviceGroupRelPO.getDeviceIds();
        for (Integer deviceId : deviceIds) {
            DeviceGroupRel areaDeviceRel = deviceGroupRelServiceImpl.findByGroupIdAndDeviceId(groupId, deviceId);
            if (areaDeviceRel != null) {
                return ResponseUtil.fail(ResultCodeConst.PARAM_ERROR, "设备ID = " + deviceId + "的设备已在该组!");
            }
        }
        List<DeviceGroupRel> deviceRels = deviceIds.stream().map(deviceId -> {
            DeviceGroupRel deviceGroupRel = new DeviceGroupRel();
            CommonsUtil.initEntity(deviceGroupRel);
            deviceGroupRel.setGroupId(groupId);
            deviceGroupRel.setDeviceId(deviceId);
            return deviceGroupRel;
        }).collect(Collectors.toList());
        boolean b = deviceGroupRelServiceImpl.saveOrUpdateBatch(deviceRels);
        return ResponseUtil.success(b);
    }

    @Override
    public ApiResponse<List<DeviceInfoVO>> getRelDevices(Integer groupId) {
        List<DeviceInfoVO> devices = deviceGroupMapper.getDevicesByGroupId(groupId);
        return ResponseUtil.success(devices);

    }

    @Override
    public ApiResponse<Boolean> delRelDevice(Integer groupId, Integer deviceId) {
        DeviceGroupRel deviceGroupRel = deviceGroupRelServiceImpl.findByGroupIdAndDeviceId(groupId, deviceId);
        if (null != deviceGroupRel) {
            deviceGroupRel.setState(CommonConst.STATE.INVALID);
            boolean b = deviceGroupRelServiceImpl.updateById(deviceGroupRel);
            return ResponseUtil.success(b);
        }
        return ResponseUtil.fail("数据不存在");
    }
}
